package com.exam.service.impl;

import com.exam.mapper.AdminMapper;
import com.exam.mapper.LoginUserMapper;
import com.exam.mapper.StudentMapper;
import com.exam.mapper.TeacherMapper;
import com.exam.pojo.Admin;
import com.exam.pojo.LoginUser;
import com.exam.pojo.Student;
import com.exam.pojo.Teacher;
import com.exam.service.LoginService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private LoginUserMapper loginUserMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private AdminMapper adminMapper;
    @Override
    public Student loginStu(String username, String password, String role) {
        Student student = null;
        Integer id = loginUserMapper.login(username, password, role);
        if (id != null) {
            student = studentMapper.queryByUserId(id);
        }
        return student;
    }
    @Override
    public Teacher loginTea(String username, String password, String role) {
        Teacher teacher = null;
        Integer id = loginUserMapper.login(username, password, role);
        if (id != null){
            teacher = teacherMapper.queryByUserId(id);
        }
        return teacher;
    }

    @Override
    public Admin loginAdmin(String username, String password, String role) {
        Admin admin = null;
        Integer id = loginUserMapper.login(username, password, role);
        if (id != null){
            admin = adminMapper.queryByUserId(id);
        }
        return admin;
    }

    @Override
    public Boolean updatePwd(LoginUser newLoginUser) {
        LoginUser oldLoginUser = loginUserMapper.queryByName(newLoginUser.getUsername());
        if (oldLoginUser.getPassword().equals(newLoginUser.getPassword())){//新密码与旧密码相同
            return false;
        }else {
            loginUserMapper.updatePwdByName(newLoginUser);
            return true;
        }
    }



}
